package de.klingbeil.hhc.core.util;

import java.util.Collection;
import java.util.Map;

public class Assert {

	/**
	 * Helper method that checks that the given parameter object is not null.
	 * Intended to be used to assert parameters in methods or constructors.
	 * 
	 * @param parameter
	 *            The parameter object that must not be null.
	 * @param parameterName
	 *            The variable name of the object that must not be null.
	 * 
	 * @throws IllegalArgumentException
	 *             If the parameter object is null.
	 */
	public static void isNotNull(Object parameter, String parameterName) {
		if (parameter == null) {
			String msg = String.format("Parameter %s must not be null.",
					parameterName);
			throw new IllegalArgumentException(msg);
		}
	}

	/**
	 * Helper method that checks whether the given string parameter is not
	 * blank. This includes <code>String</code> instances that are null or
	 * consist only of spaces and whitespaces. Intended to be used to assert
	 * parameters in methods or constructors.-
	 * 
	 * @param parameter
	 *            The string value that must not be null.
	 * @param parameterName
	 *            The variable name of the parameter that is checked.
	 * 
	 * @throws IllegalArgumentException
	 *             If the parameter object is null, empty or only whitespaces.
	 */
	public static void isNotBlank(String parameter, String parameterName) {
		if (parameter == null || "".equals(parameter.trim())) {
			String pattern = "Parameter %s must not be null, empty or a whitespace sequence.";
			throw new IllegalArgumentException(String.format(pattern,
					parameterName));
		}
	}

	private Assert() {
		// prevent instance creation
	}

	public static void isNotNullOrEmpty(Collection<?> collection,
			String parameterName) {
		if (collection == null || collection.isEmpty()) {
			String pattern = "Parameter %s must not be null or empty.";
			throw new IllegalArgumentException(String.format(pattern,
					parameterName));
		}
	}

	public static void isNotNullOrEmpty(Map<?, ?> map, String parameterName) {
		if (map == null || map.isEmpty()) {
			String pattern = "Parameter %s must not be null or empty.";
			throw new IllegalArgumentException(String.format(pattern,
					parameterName));
		}
	}
}
